package com.tanhua.server.controller;

import com.tanhua.commons.exception.TanHuaException;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.VideosService;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
@RequestMapping("/smallVideos")
public class VideosController {

    @Autowired
    private VideosService videosService;

    /**
     * 发布小视频
     *
     * @param videoThumbnail
     * @param videoFile
     * @return
     */
    @PostMapping
    public ResponseEntity post(MultipartFile videoThumbnail, MultipartFile videoFile) {

        videosService.post(videoThumbnail, videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 获取小视频列表
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity getVideos(Long page, Long pagesize) {
        PageResult result = videosService.getVideos(page, pagesize);
        return ResponseEntity.ok(result);
    }

    /**
     * 视频用户关注
     *
     * @param uid
     * @return
     */
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable Long uid) {
        if (uid.longValue() == UserHolder.getUserId().longValue()) {
            throw new TanHuaException("不可以关注自己哦");
        }
        videosService.userFocus(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频用户关注 - 取消
     *
     * @param uid
     * @return
     */
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable Long uid) {
        videosService.userUnFocus(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频点赞
     *
     * @param id
     * @return
     */
    @PostMapping("/{id}/like")
    public ResponseEntity likeVideo(@PathVariable String id) {
        videosService.likeVideo(id);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频点赞 - 取消
     *
     * @param id
     * @return
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity disLike(@PathVariable String id) {
        videosService.disLike(id);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/{id}/comments")
    public ResponseEntity comments(@PathVariable ObjectId id, PageResult page){

        PageResult result = videosService.getComments(id,page);
        return ResponseEntity.ok(result);
    }
}
